package com.rui.cn.provider.controller.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.rui.cn.entity.system.SysMenu;
import com.rui.cn.entity.system.SysPermission;
import com.rui.cn.provider.service.system.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysmenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping(value = "/menuList/{id}")
    public List<SysMenu> menuList(@PathVariable("id") Integer id){
        return  sysMenuService.menuList(id);
    }

    @GetMapping(value = "/getMenuInfo/{id}")
    public SysMenu getMenuInfo(@PathVariable("id") Integer id){
        return  sysMenuService.getMenuInfo(id);
    }

    @GetMapping(value = "/getRoleMenu/{roleid}")
    public List<SysMenu> getRoleMenu(@PathVariable("roleid") Integer roleid){
        return  sysMenuService.getRoleMenu(roleid);
    }

    @PostMapping(value = "/selectList")
    public  List<SysMenu> selectList(){
        return  sysMenuService.selectList(new Wrapper<SysMenu>() {
            @Override
            public String getSqlSegment() {
                return null;
            }
        });
    }

    @PostMapping(value = "/insertOrUpdate")
    public  boolean insertOrUpdate(@RequestBody SysMenu sysMenu){
        return  sysMenuService.insertOrUpdate(sysMenu);
    }

    @DeleteMapping(value ="/deleteById/{id}" )
    private void deleteById(@PathVariable("id") Integer id){
        sysMenuService.deleteById(id);
    }

    @GetMapping(value = "/selectById/{id}")
    public SysMenu selectById(@PathVariable("id") Integer id){
        return  sysMenuService.selectById(id);
    }

    @GetMapping(value = "/selectListByparentId/{parentid}")
    public List<SysMenu> selectListByparentId(@PathVariable("parentid") Integer parentid){
        return sysMenuService.selectList(new Wrapper<SysMenu>() {
            @Override
            public String getSqlSegment() {
                return "where parentid="+parentid;
            }
        });
    }
    @GetMapping(value = "/selectByparentOrid/{id}")
    public List<SysMenu> selectByparentOrid(@PathVariable("id") String id){
        EntityWrapper<SysMenu> ew=new EntityWrapper<>();
        ew.setEntity(new SysMenu());
        if(StringUtils.isNotBlank(id) && !id.equals("0")){
            ew.where("id={0}",id).or("parentid={0}",id);
        }
        return sysMenuService.selectList(ew);
    }

}
